<?php

namespace App\Services;

use App\Repositories\Contracts\BranchInfoRepository;

class BranchInfoService
{
    private $branchInfoRepository;

    public function __construct(BranchInfoRepository $branchInfoRepository)
    {
        $this->branchInfoRepository = $branchInfoRepository;
    }

    /**
     * 获取列表
     *
     * @param array|null $param
     */
    public function getList($param = null)
    {
        try {
            $filter = [];
            if (isset($param['branch_id']) && $param['branch_id'] != 0) {
                $filter = [
                    'branch_id' => $param['branch_id'],
                ];
            }

            $result = $this->branchInfoRepository->paginate($param['per_page'] ?? null, $filter);
            return $result;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 获取详情
     *
     * @param int $id
     * @return object|array
     */
    public function getInfo($id)
    {
        try {
            $result = $this->branchInfoRepository->find($id);
            return $result;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 新增
     *
     * @param array $param
     */
    public function add($param)
    {
        try {
            $branchInfo = $this->branchInfoRepository->store($param);
            return $branchInfo;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 修改
     *
     * @param array $param
     * @param int $id
     */
    public function update($param, $id)
    {
        try {
            $branchInfo = $this->branchInfoRepository->update($param, $id);
            return $branchInfo;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 删除
     *
     * @param int $id
     */
    public function delete($id)
    {
        try {
            $branchInfo = $this->branchInfoRepository->delete($id);

            $result = [];
            if ($branchInfo) {
                $result = 'success';
            } else {
                $result = 'error';
            }
            return $result;

        } catch (\Throwable $th) {
            throw $th;
        }
    }
}
